package com.hc.acoustics.ui.activity

import android.content.Intent
import android.os.Bundle
import com.hc.acoustics.R
import com.hc.acoustics.base.BaseActivity
import com.hc.acoustics.util.FormulaUtil
import com.yc.yclibrary.base.YcAppCompatActivity
import kotlinx.android.synthetic.main.formula_button.*
import kotlinx.android.synthetic.main.type2_formula4_location_activity.*
import kotlinx.android.synthetic.main.type6_formula2_location_activity.*
import kotlinx.android.synthetic.main.type6_formula3_location_activity.*
import kotlinx.android.synthetic.main.type6_formula4_location_activity.*
import kotlinx.android.synthetic.main.type6_formula6_location_activity.*
import kotlinx.android.synthetic.main.type7_formula3_location_activity.*
import kotlinx.android.synthetic.main.type7_formula3_location_activity.radioButton
import kotlinx.android.synthetic.main.type7_formula3_location_activity.radioButton2
import java.lang.NumberFormatException

/**
 *  声学增益 - 开启话筒数量NOM
 */
class Type7Formula3Activity : BaseActivity() {
    companion object {
        private const val KEY_TITLE = "key_title"
        @JvmStatic
        fun newInstance(activity: YcAppCompatActivity, title: String) {
            val intent = Intent(activity, Type7Formula3Activity::class.java)
            intent.putExtra(KEY_TITLE, title)
            activity.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.type7_formula3_location_activity
    }

    override fun initView(savedInstanceState: Bundle?) {
        setActionBar(intent.getStringExtra(KEY_TITLE))
        formulaResetBtn.setOnClickListener {
            formula7_3_a_TEV.reset()
            formula7_3_b_TEV.reset()
            formula7_3_c_TEV.reset()
            formula7_3_d_TEV.reset()
            formula7_3_e_TEV.reset()
        }
        formulaOkBtn.setOnClickListener {
            try {
                val resultB = FormulaUtil.formula7_3_1(formula7_3_a_TEV.getDouble())
                var resultE: Double?
                if (radioButton.isSelect()) {
                    resultE = FormulaUtil.formula7_3_2(
                        resultB,
                        formula7_3_c_TEV.getDouble(),
                        6
                    )
                } else {
                    resultE = FormulaUtil.formula7_3_2(
                        resultB,
                        formula7_3_c_TEV.getDouble(),
                        12
                    )
                }
                formula7_3_d_TEV.setText(resultB)
                formula7_3_b_TEV.setText(resultB)
                formula7_3_e_TEV.setText(resultE)
            } catch (e: NumberFormatException) {
                formula7_3_d_TEV.setText("Infinity")
                formula7_3_b_TEV.setText("Infinity")
                formula7_3_e_TEV.setText("Infinity")
            }
        }
        radioButton.setOnClickListener {
            radioButton.setSelect(!radioButton.isSelect())
            radioButton2.setSelect(!radioButton2.isSelect())
        }
        radioButton2.setOnClickListener {
            radioButton.setSelect(!radioButton.isSelect())
            radioButton2.setSelect(!radioButton2.isSelect())
        }
    }
}